# -*- coding: utf-8 -*-
# import matplotlib 
# matplotlib.use("tkagg") 

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import fake
import single
import muti2

fig, ax = plt.subplots()
ax.set_xlim(0, single.WIDTH)
ax.set_ylim(0, single.HEIGHT)
ax.set_aspect(1)

plt.scatter(
    single.obstacle_points.data[:, 0], 
    single.obstacle_points.data[:, 1]
)

def onclick(event):
    global manager
    if not hasattr(manager.escaper, "OnClick"):
        return
    manager.escaper.OnClick(event.xdata, event.ydata)
    pass
cid = fig.canvas.mpl_connect('button_press_event', onclick)

elp_debug, = plt.plot([], [], "yo")
aim_debug, = plt.plot([], [], "go")
escaper, = plt.plot([], [], "ro")
purser, = plt.plot([], [], "bo")

manager = fake.Test()
isQuit = False

def Update(iframe):
    global isQuit
    if not isQuit:
        isQuit = manager.IsQuit()
        if isQuit:
            print("Get it !!!!!" * 100)
            manager.escaper.Stop()
        else:
            manager.MainLoop()
            fake.Update(manager)
    e = manager.escaper.GetPos()
    escaper.set_data([e[0]], [e[1]])
    px = []
    py = []
    for p in manager.pursers:
        pos = p.GetPos()
        px.append(pos[0])
        py.append(pos[1])
    purser.set_data(px, py)
    px = []
    py = []
    for p in manager.pursers + [manager.escaper]:
        if hasattr(p, "target") and p.target is not None:
            px.append(p.target[0])
            py.append(p.target[1])
    elp_debug.set_data(px, py)
    px = []
    py = []
    for p in manager.pursers:
        if hasattr(p, "aim") and p.aim is not None:
            px.append(p.aim[0])
            py.append(p.aim[1])
    aim_debug.set_data(px, py)
    pass
ani = FuncAnimation(fig, Update)

plt.pause(0.01)
plt.draw

plt.show()